package org.putrpctorest.expression;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.putrpctorest.PutRpcToRestError;

public class RestMethodEntryPoint {
	
	private String id;
	
	private List<Expression> serviceRouters = new ArrayList<Expression>();
	
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	public RestMethodEntryPoint(List<Expression> serviceRouters) {
		this.serviceRouters = new ArrayList<Expression>(serviceRouters);
	}
	
	public Object invoke(Map<String,Object> params) throws Exception {
		ExpressionResult routerResult;
		for(Expression serviceRouter:serviceRouters) {
			routerResult = serviceRouter.route(params);
			if (! routerResult.isSkiped()) {
				return routerResult.getResult();
			}
		}
		throw new PutRpcToRestError("None of the routers could route the rest method call!");
	}
	
	public void addServiceRouter(Expression serviceRouter) {
		this.serviceRouters.add(serviceRouter);
	}
	
}
